package com.carbon.config;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.carbon.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 自动填充
 *
 * @author WangFK
 * @version 1.0
 * @since 2023/8/22 17:08
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject,"createBy", String.class, SecurityUtils.getUsername());
        this.strictInsertFill(metaObject, "createTime", Date.class, DateUtil.date()); // 起始版本 3.3.0(推荐使用)
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject,"updateBy", String.class, SecurityUtils.getUsername());
        this.strictUpdateFill(metaObject, "updateTime", Date.class, DateUtil.date());
    }
}
